var noOfTabActivityInfo=4,noOfTabFinance=3;

function tabSwitch(active, number) {  
  
    for (var i=1; i <= number; i++) {  
      document.getElementById('content_'+i).style.display = 'none';  
      document.getElementById('tab_'+i).className = '';  
    }  
    document.getElementById('content_'+active).style.display = 'block';  
    document.getElementById('tab_'+active).className = 'active';      
  
}  

function openActivityItem()
{
	document.getElementById('divToday').style.display = 'none'; 
	document.getElementById('divActivityTaskInfo').style.display = 'block'; 
	for (var i=1; i <= noOfTabActivityInfo; i++) {    
	      document.getElementById('tabActivityInfo_'+i).className = '';  
	      document.getElementById('contentActivityInfo_'+i).className = 'tabActivityInfoContent';       
	}
	document.getElementById('tabActivityInfo_1').className = 'active';
	document.getElementById('contentActivityInfo_1').className = 'tabActivityInfoContentActive';   
	document.getElementById('contentActivityInfo_1').style.display = 'block';  
	for (var i=1; i <= noOfTabFinance; i++) {   
	    	document.getElementById('tabFinance_'+i).className = ''; 
	    	document.getElementById('contentFinance_'+i).className = 'tabFinanceContent';
	    }
	document.getElementById('tabFinance_1').className = 'active'; 
	document.getElementById('contentFinance_1').className = 'tabFinanceContentActive';
	document.getElementById('contentFinance_1').style.display = 'block';  
	
	$(".tabActivityInfoContent").css("display","none");
	$(".tabFinanceContent").css("display","none");
	
}

function closeActivityItem()
{
	document.getElementById('divActivityTaskInfo').style.display = 'none'; 
	document.getElementById('divToday').style.display = 'block'; 
}

function tabSwitchFinance(active) {  
	 
    for (var i=1; i <= noOfTabFinance; i++) {  
    	document.getElementById('contentFinance_'+i).style.display = 'none';  
    	document.getElementById('tabFinance_'+i).className = ''; 
    }
    document.getElementById('contentFinance_'+active).style.display = 'block';  
	document.getElementById('tabFinance_'+active).className = 'active'; 
}  

function tabSwitchActivityInfo(active) {  
	  
    for (var i=1; i <= noOfTabActivityInfo; i++) {  
      document.getElementById('contentActivityInfo_'+i).style.display = 'none';  
      document.getElementById('tabActivityInfo_'+i).className = '';  
      document.getElementById('contentActivityInfo_'+i).className = 'tabActivityInfoContent'; 
    }  
    document.getElementById('contentActivityInfo_'+active).style.display = 'block';  
    document.getElementById('tabActivityInfo_'+active).className = 'active';   
    document.getElementById('contentActivityInfo_'+active).className = 'tabActivityInfoContentActive';
  
}

function tabSwitchInHome(active, number) {  
	  
    for (var i=1; i <= number; i++) {  
      document.getElementById('content_'+i).style.display = 'none';  
      document.getElementById('tab_'+i).className = '';  
    }  
    document.getElementById('content_'+active).style.display = 'block';  
    document.getElementById('tab_'+active).className = 'active';  
    if(active==1)
    {
    	$(".tabs").css("display","none");
    }
    else
    {
    	$(".tabs").css("display","block");
    }
  
}  

function tabSwitchActivity(active, number) {  
	document.getElementById('divActivity').style.display = 'block'; 
    for (var i=1; i <= number; i++) {  
    	document.getElementById('contentActivityHeader_'+i).style.display = 'none'; 
      document.getElementById('contentActivity_'+i).style.display = 'none';  
      
      
   //   document.getElementById('tab_'+i).className = '';  
    }  
    document.getElementById('divContentLeft').style.display = 'none';
    document.getElementById('contentActivitySelect').style.display = 'block';
    document.getElementById('contentActivityHeader_'+active).style.display = 'block';
    document.getElementById('contentActivity_'+ active).style.display = 'block';  
    document.getElementById('optionActivity_' + active).selected='true' ;  
     
   
  
}  



function tabBackProject(number) {  
	closeActivityItem();
	 for (var i=1; i <= number; i++) {  
	      document.getElementById('contentActivity_'+i).style.display = 'none';   
	    }  
	 document.getElementById('contentActivitySelect').style.display = 'none';
	 document.getElementById('divActivity').style.display = 'none'; 
	 document.getElementById('divContentLeft').style.display = 'block';   

}

function initTab()
{
	$(".tabContent").css("display","none");
}



var NUMBER_OF_TAB 	= 4;

var HOME_TAB 		= 1;
var EMAIL_TAB 		= 2;
var CALENDAR_TAB 	= 3;
var FORUM_TAB 		= 4;

////////////////////////////////////////////////////////////
//Helpers
////////////////////////////////////////////////////////////
var TAB_SELECTION_MASK 	= 0xFF; 
var selectedTabs 		= 0x00;

function resetTabSelection()
{
	selectedTabs = 0x00;
}

function setTabSelect(iTab)
{
	var iTabFlag = Math.pow(2, iTab - 1);
	selectedTabs |= iTabFlag;
}

function setTabUnSelect(iTab)
{
	var iTabFlag = Math.pow(2, iTab - 1);
	selectedTabs &= ((~iTabFlag) & TAB_SELECTION_MASK);
}

function isTabSelected(iTab)
{
	var iTabFlag = Math.pow(2, iTab - 1);
	return ((selectedTabs & (TAB_SELECTION_MASK & iTabFlag)) == iTabFlag);
}
//
////////////////////////////////////////////////////////////


function returnHomePage()
{
	//calendar
	restoreWindow(CALENDAR_TAB);

	//email
	restoreWindow(EMAIL_TAB);

	tabSwitchInHome(1, NUMBER_OF_TAB);
	resetTabSelection();
	return false;
}

function maximizeWindow(iTab)
{
	if (iTab == CALENDAR_TAB)
	{
		//////////////////////////////////////////
		//Move the calendar to the corrected tag
		//////////////////////////////////////////
		var calendarContent = $('#divCalendar');
		$('#minimizedCalendarHeader').hide();
		$('#divCalendarMaximizedPanel').prepend(calendarContent);
 		$('.divCalendarMaximize').toggle();//('slow');		
		tabSwitchInHome(CALENDAR_TAB, NUMBER_OF_TAB);
	}
	else if (iTab == EMAIL_TAB)
	{
		var emailNav = $('#divEmailNavigation');
		$('#divMaximizedEmailNavigationContainer').prepend(emailNav);
		
		var emailList = $('#divEmailList');
		$('#divMaximizedEmailListContainer').prepend(emailList);
		$(".emailSender", emailList).show();
		$(".emailDate").toggleClass("emailDate emailMaximizedDate");
				
		var emailDisplayer = $('#divEmailDisplay');
		$('#divEmailMaximizedDisplayContainer').prepend(emailDisplayer);

		var emailHeader = $("#divEmailHeader");
		$("#divEmailMaximizedHeader").prepend(emailHeader);
		// 
		tabSwitchInHome(EMAIL_TAB, NUMBER_OF_TAB);

		$('#divEmailMaximizedListPanelScrollbar').tinyscrollbar();
		$('#divEmailMaximizedListPanelScrollbar').tinyscrollbar_update();
		$('#emailMaximizedVerticalScrollbar').tinyscrollbar();
		
		//Init splitter for maximized email panel
		var wasSplit = false;
		if ($("#divEmailMaximizedSplitterContainer .vsplitbar").length > 0)
		{		
			wasSplit = true;
		}	
				
		if (wasSplit == false)
		{	
			$("#divEmailMaximizedSplitterContainer").splitter({
				splitVertical: true,
				sizeLeft: true,
				accessKey: 'I'
			});
		}
	}
	else if (iTab == FORUM_TAB)
	{
		tabSwitchInHome(FORUM_TAB, NUMBER_OF_TAB);			
	}
	
	setTabSelect(iTab);
	return false;
}

function minimizeWindow(iTab)
{
	var backToHomePage = true;
	
	setTabUnSelect(iTab);
	
	//Try to maximize another tab
	if (isTabSelected(FORUM_TAB))
	{
		tabSwitch(FORUM_TAB, NUMBER_OF_TAB);
		backToHomePage = false;
	}else if (isTabSelected(CALENDAR_TAB))
	{
		tabSwitch(CALENDAR_TAB, NUMBER_OF_TAB);
		backToHomePage = false;
	}
	else if (isTabSelected(EMAIL_TAB))
	{
		tabSwitch(EMAIL_TAB, NUMBER_OF_TAB);
		backToHomePage = false;						
	}

	if (backToHomePage == true)
	{
		returnHomePage();		
	}
	return false;
}

function restoreWindow(iTab)
{
	if (iTab == CALENDAR_TAB)
	{			
		//////////////////////////////////////////
		//Move the calendar back to the corrected tag
		//////////////////////////////////////////
		var calendarContent = $('#divCalendar');
		$('#minimizedCalendarHeader').show();
		$('#divCalendarMinimizedPanel').prepend(calendarContent);			
	}
	else if (iTab == EMAIL_TAB)
	{
		var emailNav = $('#divEmailNavigation');
		$('#divMinimizedEmailNavigationContainer').prepend(emailNav);
		
		var emailList = $('#divEmailList');
		$('#divMinimizedEmailListContainer').prepend(emailList);
		$(".emailSender", emailList).hide();
		$(".emailMaximizedDate").toggleClass("emailDate emailMaximizedDate");
		
		var emailDisplayer = $('#divEmailDisplay');
		$('#divEmailMinimizedDisplayContainer').prepend(emailDisplayer);
		
		var emailHeader = $("#divEmailHeader");
		$("#divEmailMinimizedHeader").prepend(emailHeader);

	}
	else if (iTab == FORUM_TAB)
	{
		
	}
	return false;
}

function trActivityMouseOver(task,tr)
{
	$("#trActivityTaskContent"+task+"_"+tr).css("background-color","RGB(246,139,61)");
}

function trActivityMouseOut(task,tr)
{
	$("#trActivityTaskContent"+task+"_"+tr).css("background-color","white");
}

